package com.zbcbms.controller;

import com.zbcbms.common.AlertUtilCommon;
import com.zbcbms.common.GetChartCommon;
import com.zbcbms.entity.tableData.BookBorrowTableData;
import com.zbcbms.service.impl.BookBorrowServiceImpl;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.PieChart;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author fengq
 */
public class RecycleBorrowController implements Initializable {
    @FXML
    private TableView<BookBorrowTableData> searchBookBorrowTableView;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchId;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchUsername;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchBorrowTime;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchReturnTime;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchBookName;
    @FXML
    private TableColumn<BookBorrowTableData,String> OperationName;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchStatus;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchBookNumber;
    @FXML
    private TableColumn<BookBorrowTableData,String> searchUserNumber;
    @FXML
    private TableColumn operation;
    @FXML
    private LineChart<String, Long> BorrowCountLineChart;
    @FXML
    private PieChart bookStatusPieChart;

    private GetChartCommon getChartCommon=new GetChartCommon();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
       bookBorrowTableInit(searchBookBorrowTableView,
                searchId,searchUsername,searchBorrowTime,searchReturnTime,searchBookName,
                OperationName,searchStatus,searchBookNumber,searchUserNumber,operation);
    }

    public void bookBorrowTableInit(TableView<BookBorrowTableData> tableView,
                                    TableColumn<BookBorrowTableData,String> searchId,
                                    TableColumn<BookBorrowTableData,String> searchUsername,
                                    TableColumn<BookBorrowTableData,String> searchBorrowTime,
                                    TableColumn<BookBorrowTableData,String> searchReturnTime,
                                    TableColumn<BookBorrowTableData,String> searchBookName,
                                    TableColumn<BookBorrowTableData,String> OperationName,
                                    TableColumn<BookBorrowTableData,String> searchStatus,
                                    TableColumn<BookBorrowTableData,String> searchBookNumber,
                                    TableColumn<BookBorrowTableData,String> searchUserNumber,
                                    TableColumn operation){
        searchId.setCellValueFactory(cellData->cellData.getValue().getId());
        searchUsername.setCellValueFactory(cellData->cellData.getValue().getUserName());
        searchBorrowTime.setCellValueFactory(cellData->cellData.getValue().getBorrowDate());
        searchReturnTime.setCellValueFactory(cellData->cellData.getValue().getReturnDate());
        searchBookName.setCellValueFactory(cellData->cellData.getValue().getBookName());
        OperationName.setCellValueFactory(cellData->cellData.getValue().getOperatorName());
        searchUserNumber.setCellValueFactory(cellData->cellData.getValue().getUserNumber());
        searchBookNumber.setCellValueFactory(cellData->cellData.getValue().getBookNumber());
        searchStatus.setCellValueFactory(cellData->cellData.getValue().getStatus());
        operation.setCellFactory((col)->{
            TableCell<BookBorrowTableData,String> cell=new TableCell<BookBorrowTableData,String>(){
                public HBox addButton=new HBox();
                Label delBtn=new Label("删除");
                Label editBtn=new Label("恢复");
                Label empty = new Label("   ");
                {
                    addButton.getStylesheets().add("view/css/user.css");
                    addButton.setAlignment(Pos.CENTER);
                    delBtn.getStyleClass().addAll("delBtn","btn");
                    editBtn.getStyleClass().addAll("editBtn","btn");
                    addButton.getChildren().addAll(editBtn,empty,delBtn);
                }
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (!empty){
                        delBtn.setOnMouseClicked((m)->{
                            BookBorrowTableData selectData=getTableView().getSelectionModel().getSelectedItem();
                            if (new AlertUtilCommon().AlterMessageOption(delBtn.getScene().getWindow(),"是否永久删除该记录？",
                                    "确认删除","取消删除","删除提示","小仲书：删除将无法恢复")){
                                new BookBorrowServiceImpl().pjySicsDeleteById(selectData.getId().get());
                                bookBorrowTableInit(tableView,
                                        searchId,searchUsername,searchBorrowTime,searchReturnTime,searchBookName,
                                        OperationName,searchStatus,searchBookNumber,searchUserNumber,operation);
                            }
                        });
                        editBtn.setOnMouseClicked((m)->{
                            if (new AlertUtilCommon().AlterMessageOption(delBtn.getScene().getWindow(),"是否要恢复该借阅记录？",
                                    "确认恢复","取消恢复","恢复提醒","小仲书：恢复将吧给记录加会表中")){
                                BookBorrowTableData selectData=getTableView().getSelectionModel().getSelectedItem();
                                new BookBorrowServiceImpl().recoverById(selectData.getId().get());
                                bookBorrowTableInit(tableView,
                                        searchId,searchUsername,searchBorrowTime,searchReturnTime,searchBookName,
                                        OperationName,searchStatus,searchBookNumber,searchUserNumber,operation);
                            }
                        });
                        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                        setGraphic(addButton);
                    }else {
                        setGraphic(null);
                    }
                }
            };
            return cell;
        });
        tableView.setItems(new BookBorrowServiceImpl().getDeletedBorrow());
    }
}
